

function getBase64Image(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img,0 ,0 , img.width, img.height);
    var dataURL = canvas.toDataURL("image/png");  // 可选其他值 image/jpeg
    return dataURL;
}

function main(src,cb) {
    var image = new Image();
    image.src = src + '?v=' + Math.random(); // 处理缓存
    image.crossOrigin = "*";  // 支持跨域图片
    image.onload = function(){
        var base64 = getBase64Image(image);
        cb && cb(base64);
    }
}

function renderPdf(type){
    var dom = document.getElementById("contn");
    var width = dom.offsetWidth; //获取dom 宽度
    var height = dom.offsetHeight; //获取dom 高度
    var offsetTop = dom.offsetTop; 
    var scaleBy = 1;
    var canvas = document.createElement('canvas');
    canvas.width = width * scaleBy; //定义canvas 宽度 * 缩放
    canvas.height = (height + offsetTop) * scaleBy; //定义canvas（高度 +偏移量 ）*缩放
    canvas.style.width = width * scaleBy + 'px';
    canvas.style.height = (height + offsetTop) * scaleBy + 'px';
    var now = new Date();
    var filename = now.getFullYear() + '.' + (now.getMonth() + 1) + '.' + now.getDate() + ".png";
    if(type) filename = type;
    html2canvas(dom, {
        background:"#ffffff",
        canvas: canvas,
        useCORS: true,
        scale: 1, 
        width: width, 
        height: height
    }).then(function(canvas){
        var type ='png';
        var imgData =canvas.toDataURL(type);
        var _fixType =function(type) {
            type = type.toLowerCase().replace(/jpg/i, 'jpeg');
            var r = type.match(/png|jpeg|bmp|gif/)[0];
            return 'images/' + r;
        };
        imgData = imgData.replace(_fixType(type),'image/octet-stream');
        var saveFile = function(data, filename){
            var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
            save_link.href = data;
            save_link.download = filename;
            var event = document.createEvent('MouseEvents');
            event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            save_link.dispatchEvent(event);
        };
        saveFile(imgData,filename);
    })
　　　}